<?php
/**
 * 签到
 */

namespace app\api\model;
use think\Model;

use think\Db;

class Sign extends Base
{
    
    /**
    * 会员签到 
    * @param mid int 会员id
    */
    public function _signIn($mid, $day){
        $model = new Member;
        $member = $model->_getInfoById($mid);
        $udid = $member['udid'];
        $num = $member['sign_continuity_seven_num'];//七天连续签到次数

     
        
        $model = new MemberInfo;
        $memberInfo = $model->_getInfoByMid($mid);
        $time = time();
        
        /*if($memberInfo['sign_continuity_seven_num'] == 7){
            if($day != 1){
                $this->error = '签到失败';
                return false;
            }
        }elseif($memberInfo['sign_continuity_seven_num'] < 7){
            if($day != $memberInfo['sign_continuity_seven_num'] + 1){
                $this->error = '签到天数错误';
                return false;
            }
        }删除*/
        
        $continue_num = $memberInfo['sign_continuity_num'] + 1;
        $continue_seven_num = $memberInfo['sign_continuity_seven_num'] + 1;
        if($continue_seven_num > 7){
            $continue_seven_num = 1;
        }
        
        $last_time = $member['last_sign_time'];
        
        
        
        if(!empty($last_time)){
            
            
            $ww = 48 * 3600;
            
            if($time - $last_time < 24 * 3600){
                $this->error = '未到签到时间';
                return false;
            }
            
            if($time - $last_time > $ww){ //若超过48小时,则算重新签到 按第一次签到算
                $continue_num = 1;
                $continue_seven_num = 1;
            }
        }else{
            $continue_num = 1;
            $continue_seven_num = 1;
        }
        
        $setModel = new SignSet;
        $sign_key = $continue_seven_num;
        $signSetInfo = $setModel->_getInfoByKey($sign_key);
        if(empty($signSetInfo)){
            $this->error = '签到设置错误';
            return false;
        }
        
        $gold = $signSetInfo['gold'];
        $this->startTrans();
        
        $update = [
            'sign_continuity_num' => $continue_num,
            'sign_continuity_seven_num' => $continue_seven_num,
            'sign_num' => $memberInfo->sign_num + 1,
        ];
        $ret = $model->_update([['mid', '=', $mid]], $update);
        if(!$ret){
            $this->error = '签到天数设置失败';
            return false;
        }
        
        //写入签到表，写入金币记录

        
        $date = date("Y-m-d");
        $sql = "insert sy_sign(mid, udid, day_num, date, created) select $mid, '$udid', $continue_num, '$date', $time from sy_member_info where sign_continuity_seven_num = $continue_seven_num and mid = $mid";
        //echo $sql;die;
        $ret = Db::execute($sql);
        if(!$ret){
            $this->error = '签到失败';
            return false;
        }
        
        $sys = $this->getConfig();
        
        $sql = "update sy_member_info set total_gold = total_gold + $gold where mid = $mid and sign_continuity_seven_num = $continue_seven_num";
        //echo $sql;die;
        $ret = Db::execute($sql);
        if(!$ret){
            $this->error = '签到失败';
            return false;
        }
        
        $sql = "update sy_member set gold = gold + $gold, last_sign_time = $time where id = $mid and (select sign_continuity_seven_num from sy_member_info where mid = $mid) = $continue_seven_num";
        //echo $sql;die;
        $ret = Db::execute($sql);
        if(!$ret){
            $this->error = '签到失败';
            return false;
        }
        
        $serial_number = serialNumber();
        $image = $sys['DEFAULT_TASK_IMG'];
        $remark = '连续签到'.$continue_seven_num.'天';
        $type_id = 1;
        $ip = ip2long(get_ip());
        $sql = "insert sy_gold_record(mid, udid, type_id, serial_number, image, remark, gold, ip, created) select $mid, '$udid', $type_id, '$serial_number', '$image', '$remark', $gold, $ip, $time from sy_member_info where sign_continuity_seven_num = $continue_seven_num and mid = $mid";
        //echo $sql ;die;
        $ret = Db::execute($sql);
        if(!$ret){
            $this->error = '签到失败';
            return false;
        }
        
        $this->commit();
        return $gold;
        
    }
    
    /**
    * 登录时修改连续签到天数
    */
    public function _setSignContinueNum($mid){
        $last_sign = $this->where([['mid', '=', $mid]])->order("created desc")->find();
        $model = new Member;
        $member = $model->_getInfoById($mid);
        $model = new MemberInfo;
        
        $last_time = $member['last_sign_time'];
        $time = time();
        $ww = 48 * 3600;
        if($time - $last_time > $ww){ //若超过24小时则更新
            $update = [
                'sign_continuity_num' => 0,
                'sign_continuity_seven_num' => 0,
            ];
            $ret = $model->_update([['mid', '=', $mid]], $update);
            if(!$ret){
                $this->error = '连续天数重置失败';
                return false;
            }
        }
        return true;
    }

}